package com.huamao.zh.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.huamao.zh.base.SerialNumberUtils;
import com.huamao.zh.model.Account;
import com.huamao.zh.model.AccountShop;
import com.huamao.zh.model.LogCash;
import com.huamao.zh.model.LogCashFreeze;
import com.huamao.zh.model.LogKaiyuanFreeze;
import com.huamao.zh.model.LogKaiyuanTurnover;
import com.huamao.zh.model.Orders;
import com.huamao.zh.model.OrdersExtension;
import com.huamao.zh.model.OrdersStockscore;
import com.huamao.zh.model.SktSysConfigs;
import com.huamao.zh.model.Users;
import com.huamao.zh.service.AccountService;
import com.huamao.zh.service.AccountShopService;
import com.huamao.zh.service.BusinessSettleService;
import com.huamao.zh.service.FenrunService;
import com.huamao.zh.service.LogCashFreezeService;
import com.huamao.zh.service.LogCashService;
import com.huamao.zh.service.LogKaiyuanFreezeService;
import com.huamao.zh.service.LogKaiyuanTurnoverService;
import com.huamao.zh.service.OrdersExtensionService;
import com.huamao.zh.service.OrdersService;
import com.huamao.zh.service.OrdersStockscoreService;
import com.huamao.zh.service.SysConfigsService;
import com.huamao.zh.service.UsersService;

@Service
@Transactional
public class BusinessSettleServiceImpl implements BusinessSettleService {

	@Autowired
	private OrdersExtensionService ordersExtensionService;

	@Autowired
	private OrdersService orderService;

	@Autowired
	private LogCashFreezeService logCashFreezeService;

	@Autowired
	private LogKaiyuanFreezeService logKaiyuanFreezeService;

	@Autowired
	private LogKaiyuanTurnoverService logKaiyuanTurnoverService;

	@Autowired
	private LogCashService logCashService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private AccountShopService accountShopService;

	@Autowired
	private UsersService usersService;

	@Autowired
	private SysConfigsService sysConfigsService;

	@Autowired
	private OrdersStockscoreService ordersStockscoreService;

	@Autowired
	private FenrunService fenrunService;

	/**
	 * 主流程
	 * 
	 * @throws Exception
	 */
	public void businessSettle() throws Exception {

		// LogUtil.info(LogCodeType.CODE_7002, "======enter===商家货款结算======");
		synchronized (this.getClass()) {
			Map<String, Object> orderMap = new HashMap<String, Object>();
			orderMap.put("isSettle", "1");
			orderMap.put("isAutoTake", "0");

			// 查询list大小
			int listCount = ordersExtensionService.queryListCount(orderMap);
			// LogUtil.info(LogCodeType.CODE_7002, "======listCount======" +
			// listCount);

			for (Integer i = 0; i < listCount; i += 100) {
				orderMap.put("start", i);
				orderMap.put("offset", 100);

				List<OrdersExtension> orderList = ordersExtensionService.queryListTwo(orderMap);

				// LogUtil.info(LogCodeType.CODE_7002, "======结算数量======" +
				// orderList.size());
				for (OrdersExtension o : orderList) {

					// LogUtil.info(LogCodeType.CODE_7002,
					// "======更新订单结算时间和结算状态======" + o.getOrderId());
					o.setIsSettle(9);
					ordersExtensionService.update(o);

					// 查询订单
					// LogUtil.info(LogCodeType.CODE_7002,
					// "============订单号==============" + o.getOrderId());
					Orders order = orderService.getByOrderNo(o.getOrderId());

					if (order == null) {
						// LogUtil.info(LogCodeType.CODE_7002,
						// "============订单不存在==============");
						return;
					}

					// 如果payScore大于0，则累加到skt_account_system表中
					int payScore = order.getPayScore();
					if (payScore > 0) {
						accountService.addAccountSystem(payScore);
					}
					if (order == null) {
						// LogUtil.info(LogCodeType.CODE_7002,
						// "======没有需要处理的订单======");
						continue;
					}

					// LogUtil.info(LogCodeType.CODE_7002,
					// "=========更新订单完结状态========== " + order.getOrderNo());
					order.setIsClosed(1);

					orderService.update(order);

					// 查询商店
					AccountShop accountShop = accountShopService.getByShopId(order.getShopId());

					if (accountShop == null) {
						// LogUtil.info(LogCodeType.CODE_7002,
						// "======查不到商店======");
						return;
					}

					// 查询用户 ?店铺的userId
					Users user = usersService.get(accountShop.getUserId());
					if (user == null) {
						// LogUtil.info(LogCodeType.CODE_7002,
						// "======查不到用户======");
						return;
					}

					// LogUtil.info(LogCodeType.CODE_7002, user.getUserId() +
					// "======用户类型====" + user.getUserType());
					int userType = user.getUserType();
					if (userType == 0) {
						// 普通用户处理流程
						this.normalExecution(user, order, accountShop);

					} else {
						// 非普通用户处理流程
						this.nonNormalUserExecution(user, order, accountShop);
					}

					// 下单时积分不足的情况
					int flag = o.getScoreFlag();
					// LogUtil.info(LogCodeType.CODE_7002, "======是否积分不足====" +
					// flag);
					if (flag == 2) {
						// 积分兑换流程
						this.scoreExchangeExecution(order, userType, o, user, accountShop);
					}

					// 更新处理时间
					o.setSettleTime(new Date());
					o.setIsSettle(2);
					o.setRealSettleTime(new Date());
					ordersExtensionService.update(o);

					// 发送分润
					// LogUtil.info(LogCodeType.CODE_7002, "发送分润");
					Map<String, Object> map = new HashMap<String, Object>();

					map.put("source", "B");
					map.put("userId", order.getUserId());
					map.put("shopUserId", accountShop.getUserId());
					map.put("orderNo", order.getOrderNo());
					map.put("totalMoney", order.getTotalMoney());
					map.put("totalScore", o.getScore());

					fenrunService.doFenRun(JSON.toJSONString(map));

				}
			}

			// LogUtil.info(LogCodeType.CODE_7002, "======exit===商家货款结算======");
		}
	}

	/**
	 * 积分兑换流程
	 * 
	 * @param order
	 * @param userType
	 * @param o
	 * @param user
	 * @param accountShop
	 * @throws Exception
	 */
	private void scoreExchangeExecution(Orders order, int userType, OrdersExtension o, Users user,
			AccountShop accountShop) throws Exception {

		// 积分发给消费者
		BigDecimal score = o.getScore();
		// Account userAccount = accountService.getByUserId(user.getUserId());
		// userAccount.setScore(score.add(userAccount.getScore()));

		// 获取换算比例
		SktSysConfigs config = sysConfigsService.getByFieldCode("buyStockScorePrice");
		BigDecimal fieldValue = new BigDecimal(config.getFieldValue());
		BigDecimal scoreTotal = fieldValue.multiply(score);
		BigDecimal cash = scoreTotal.divide(new BigDecimal(10000));

		int shopId = order.getShopId();
		Account account = accountService.getByShopId(shopId);
		// Account userAccount = accountService.getByUserId(order.getUserId());
		int payType = order.getPayType();

		OrdersStockscore ordersStockscore = new OrdersStockscore();

		// 扣除现金
		if (payType == 2 || payType == 4) {

			// LogUtil.info(LogCodeType.CODE_7002, "======扣除现金======");

			if (userType != 0) {
				LogCash logCash = new LogCash();
				BigDecimal preCash = account.getCash();
				// LogUtil.info(LogCodeType.CODE_7002, "======扣除现金======" +
				// cash);
				account.setCash(cash);
				account.setFreezeCash(new BigDecimal(0));
				// userAccount.setOnlineExpenditure(order.getTotalMoney());

				// accountShop.setOnlineTurnover(order.getTotalMoney());
				// accountShopService.updateOnlineTurnover(accountShop);

				// 记录一条流水日志
				logCash.setCash(cash);
				logCash.setCashType(-1);
				logCash.setType(32);
				logCash.setRemark("店铺积分不足支付订单购买库存积分扣除现金");
				if (!(cash.compareTo(BigDecimal.ZERO) == 0)) {
					addCashLog(account, user, order, logCash, preCash);
				}

			}

			if (userType == 0) {
				LogCashFreeze freezeCashLog = new LogCashFreeze();
				BigDecimal freezeCash = account.getFreezeCash();
				// LogUtil.info(LogCodeType.CODE_7002, "======扣除现金freeze======"
				// + cash);
				account.setFreezeCash(cash);
				account.setCash(new BigDecimal(0));
				// userAccount.setOnlineExpenditure(order.getTotalMoney());

				// accountShop.setOnlineTurnover(order.getTotalMoney());
				// accountShopService.updateOnlineTurnover(accountShop);

				// 记录一条流水日志
				freezeCashLog.setCash(cash);
				freezeCashLog.setType(32);
				freezeCashLog.setCashType(-1);
				freezeCashLog.setRemark("店铺积分不足支付订单购买库存积分扣除代发现金");
				if (!(cash.compareTo(BigDecimal.ZERO) == 0)) {
					addFreezeCashLog(account, user, order, freezeCashLog, freezeCash);
				}

			}

			ordersStockscore.setCash(cash);
			ordersStockscore.setTotalMoney(cash);
			ordersStockscore.setKaiyuan(new BigDecimal(0));
			// 手续费和本金分别记一条流水，手续费的流水金额是本体加上之前计算好的

			ordersStockscore.setKaiyuanFee(new BigDecimal(0));

			// 更新account中freezeCash金额
			accountService.updateExchangeCash(account);

		}

		// 获取最新的shop
		accountShop = accountShopService.getByShopId(accountShop.getShopId());

		// 华宝结算
		if (payType == 3 || payType == 6) {

			// 扣除积分
			BigDecimal scoreKaiyuan = scoreTotal.divide(new BigDecimal(100));
			SktSysConfigs kaiyuanConfig = sysConfigsService.getByFieldCode("kaiyuanturnFee");
			BigDecimal kaiyuanValue = new BigDecimal(kaiyuanConfig.getFieldValue());
			BigDecimal fee = scoreKaiyuan.multiply(kaiyuanValue).divide(new BigDecimal(100));

			// LogKaiyuanTurnover kaiyuanTurnover = new LogKaiyuanTurnover();
			// kaiyuanTurnover.setPreKaiyuan(accountShop.getTotalKaiyuanTurnover());

			if (userType != 0) {

				BigDecimal preKaiyuan = accountShop.getKaiyuanTurnover();

				BigDecimal finalKaiyuan = scoreKaiyuan.add(fee);
				accountShop.setKaiyuanTurnover(finalKaiyuan);
				accountShop.setFreezeKaiyuan(new BigDecimal(0));
				// accountShop.setOnlineTurnover(order.getTotalMoney());

				// 第一条流水
				LogKaiyuanTurnover kaiyuanTurnover = new LogKaiyuanTurnover();
				kaiyuanTurnover.setKaiyuan(scoreKaiyuan);
				kaiyuanTurnover.setType(35);
				kaiyuanTurnover.setKaiyuanType(-1);
				kaiyuanTurnover.setRemark("非普通用户华宝购买库存积分");
				if (!(scoreKaiyuan.compareTo(BigDecimal.ZERO) == 0)) {
					addKaiyuanTurnOverLog(accountShop, user, order, kaiyuanTurnover, preKaiyuan);
				}
				// 第二条流水
				LogKaiyuanTurnover kaiyuanTurnoverFee = new LogKaiyuanTurnover();
				kaiyuanTurnoverFee.setKaiyuan(fee);
				kaiyuanTurnoverFee.setType(36);
				kaiyuanTurnoverFee.setKaiyuanType(-1);
				kaiyuanTurnoverFee.setRemark("非普通用户华宝购买库存积分手续费");
				if (!(fee.compareTo(BigDecimal.ZERO) == 0)) {
					addKaiyuanTurnOverLog(accountShop, user, order, kaiyuanTurnoverFee,
							preKaiyuan.subtract(scoreKaiyuan));
				}
			}

			if (userType == 0) {

				BigDecimal freezeKaiyuan = accountShop.getFreezeKaiyuan();
				BigDecimal finalFreezeKaiyuan = scoreKaiyuan.add(fee);
				accountShop.setFreezeKaiyuan(finalFreezeKaiyuan);
				accountShop.setKaiyuanTurnover(new BigDecimal(0));
				// accountShop.setOnlineTurnover(new BigDecimal(0));

				// 第一条流水
				LogKaiyuanFreeze freezeKaiyuanObj = new LogKaiyuanFreeze();

				// LogUtil.info(LogCodeType.CODE_7002,
				// "======scoreKaiyuan======" + scoreKaiyuan);

				freezeKaiyuanObj.setKaiyuan(scoreKaiyuan);
				freezeKaiyuanObj.setType(33);
				freezeKaiyuanObj.setKaiyuanType(-1);
				freezeKaiyuanObj.setRemark("普通用户华宝购买库存积分");
				if (!(scoreKaiyuan.compareTo(BigDecimal.ZERO) == 0)) {
					addKaiyuanFreezeLog(accountShop, user, order, freezeKaiyuanObj, freezeKaiyuan);
				}

				// 第二条流水
				LogKaiyuanFreeze freezeKaiyuanObj2 = new LogKaiyuanFreeze();
				// LogUtil.info(LogCodeType.CODE_7002, "======fee======" + fee);
				freezeKaiyuanObj2.setKaiyuan(fee);
				freezeKaiyuanObj2.setType(34);
				freezeKaiyuanObj2.setKaiyuanType(-1);
				freezeKaiyuanObj2.setRemark("普通用户华宝购买库存积分手续费");
				if (!(fee.compareTo(BigDecimal.ZERO) == 0)) {
					addKaiyuanFreezeLog(accountShop, user, order, freezeKaiyuanObj2,
							freezeKaiyuan.subtract(scoreKaiyuan));
				}

			}

			/*
			 * userAccount.setCash(new BigDecimal(0));
			 * userAccount.setTotalCash(new BigDecimal(0));
			 * userAccount.setOnlineExpenditure(order.getTotalMoney());
			 * accountService.updateFormalCash(userAccount);
			 */

			ordersStockscore.setCash(new BigDecimal(0));
			ordersStockscore.setTotalMoney((scoreKaiyuan.add(fee)).divide(new BigDecimal(100)));
			ordersStockscore.setKaiyuan(scoreKaiyuan);
			ordersStockscore.setKaiyuanFee(fee);

			// 更新account中freezeCash金额
			accountShopService.updateExchangeKaiyuan(accountShop);

			// 记录业务流水数据
		}
		addOrderStockScore(user, order, account, ordersStockscore, o);

		// LogUtil.info(LogCodeType.CODE_7002, "======执行分润======");

	}

	/**
	 * 非普通用户处理流程
	 * 
	 * @param user
	 * @param order
	 * @param accountShop
	 * @throws Exception
	 */
	private void nonNormalUserExecution(Users user, Orders order, AccountShop accountShop) throws Exception {

		BigDecimal realMoney = order.getRealMoney();
		BigDecimal cash = order.getCash();
		BigDecimal kaiyuan = order.getKaiyuan();

		// 获取最新的shop
		accountShop = accountShopService.getByShopId(accountShop.getShopId());

		int payType = order.getPayType();
		// 现金结算
		if (payType == 2 || payType == 4) {

			// LogUtil.info(LogCodeType.CODE_7002,
			// "======现金结算======realMoney:" + realMoney.intValue() + "====cash:"
			// + cash.intValue());
			int shopId = order.getShopId();
			Account account = accountService.getByShopId(shopId);
			Account userAccount = accountService.getByUserId(order.getUserId());

			if (account == null) {

				// LogUtil.info(LogCodeType.CODE_7002,
				// "======account不存在======");
				return;
			}

			if (userAccount == null) {

				// LogUtil.info(LogCodeType.CODE_7002,
				// "======userAccount不存在======");
				return;
			}

			BigDecimal preCash = account.getCash();
			LogCash logCash = new LogCash();
			// logCash.setPreCash(preCash);

			if (payType == 2) {
				account.setCash(cash);
				account.setTotalCash(cash);
				account.setOnlineExpenditure(new BigDecimal(0));
				userAccount.setCash(new BigDecimal(0));
				userAccount.setTotalCash(new BigDecimal(0));
				userAccount.setOnlineExpenditure(order.getTotalMoney());
				logCash.setCash(cash);
				logCash.setRemark("订单结算现金支付非普通用户增加现金");
				logCash.setType(3);
				logCash.setCashType(1);
				if (!(cash.compareTo(BigDecimal.ZERO) == 0)) {
					addCashLog(account, user, order, logCash, preCash);
				}
			}

			if (payType == 4) {
				account.setCash(realMoney);
				account.setTotalCash(realMoney);
				account.setOnlineExpenditure(new BigDecimal(0));
				userAccount.setCash(new BigDecimal(0));
				userAccount.setTotalCash(new BigDecimal(0));
				userAccount.setOnlineExpenditure(order.getTotalMoney());
				logCash.setCash(realMoney);
				logCash.setRemark("订单结算第三方支付非普通用户增加现金");
				logCash.setType(3);
				logCash.setCashType(1);

				if (!(realMoney.compareTo(BigDecimal.ZERO) == 0)) {
					addCashLog(account, user, order, logCash, preCash);
				}
			}

			// LogUtil.info(LogCodeType.CODE_7002,
			// "======nonNormal======" + account.getAccountId() + " ======= " +
			// userAccount.getAccountId());
			accountService.updateFormalCash(account);
			accountService.updateFormalCash(userAccount);

			accountShop.setOnlineTurnover(order.getTotalMoney());
			accountShopService.updateOnlineTurnover(accountShop);

		}

		// 华宝结算
		if (payType == 3 || payType == 6) {

			// int shopId = order.getShopId();
			Account account = accountService.getByUserId(order.getUserId());
			account.setOnlineExpenditure(order.getTotalMoney());
			account.setCash(new BigDecimal(0));
			account.setTotalCash(new BigDecimal(0));
			accountService.updateFormalCash(account);

			// LogUtil.info(LogCodeType.CODE_7002, "======华宝结算======kaiyuanbao:"
			// + kaiyuan.intValue());
			BigDecimal preKaiyuan = accountShop.getKaiyuanTurnover();
			accountShop.setKaiyuanTurnover(kaiyuan);
			accountShop.setTotalKaiyuanTurnover(kaiyuan);
			accountShop.setOnlineTurnover(order.getTotalMoney());

			accountShopService.updateKaiyuanTurnover(accountShop);

			LogKaiyuanTurnover kaiyuanTurnover = new LogKaiyuanTurnover();
			kaiyuanTurnover.setKaiyuan(kaiyuan);
			kaiyuanTurnover.setType(1);
			kaiyuanTurnover.setKaiyuanType(1);
			// kaiyuanTurnover.setPreKaiyuan(accountShop.getKaiyuanTurnover());
			kaiyuanTurnover.setRemark("非普通用户华宝结算增加货款");

			if (!(kaiyuan.compareTo(BigDecimal.ZERO) == 0)) {
				addKaiyuanTurnOverLog(accountShop, user, order, kaiyuanTurnover, preKaiyuan);
			}
		}

	}

	/**
	 * 普通用户处理流程
	 * 
	 * @param user
	 * @param order
	 * @param accountShop
	 * @throws Exception
	 */
	private void normalExecution(Users user, Orders order, AccountShop accountShop) throws Exception {
		// 普通用户
		BigDecimal realMoney = order.getRealMoney();
		BigDecimal cash = order.getCash();
		BigDecimal kaiyuan = order.getKaiyuan();

		// 获取最新的shop
		accountShop = accountShopService.getByShopId(accountShop.getShopId());

		int payType = order.getPayType();
		// 现金结算
		if (payType == 2 || payType == 4) {

			// LogUtil.info(LogCodeType.CODE_7002,
			// "======现金结算======realMoney:" + realMoney.intValue() + "====cash:"
			// + cash.intValue());
			int shopId = order.getShopId();
			Account account = accountService.getByShopId(shopId);
			Account userAccount = accountService.getByUserId(order.getUserId());

			if (account == null) {
				// LogUtil.info(LogCodeType.CODE_7002,
				// "======account不存在======");
				return;
			}

			LogCashFreeze freezeCashLog = new LogCashFreeze();
			BigDecimal freezeCash = account.getFreezeCash();
			if (payType == 2) {
				account.setFreezeCash(cash);
				userAccount.setTotalCash(new BigDecimal(0));
				userAccount.setCash(new BigDecimal(0));
				userAccount.setOnlineExpenditure(order.getTotalMoney());
				freezeCashLog.setCash(cash);
				freezeCashLog.setCashType(1);
				freezeCashLog.setRemark("订单结算现金支付普通用户增加待发现金");
				freezeCashLog.setType(2); // 这里的类型要对应上，目前是2

				if (!(cash.compareTo(BigDecimal.ZERO) == 0)) {
					addFreezeCashLog(account, user, order, freezeCashLog, freezeCash);
				}
			}

			if (payType == 4) {
				account.setFreezeCash(realMoney);
				userAccount.setTotalCash(new BigDecimal(0));
				userAccount.setCash(new BigDecimal(0));
				userAccount.setOnlineExpenditure(order.getTotalMoney());
				freezeCashLog.setCash(realMoney);
				freezeCashLog.setCashType(1);
				freezeCashLog.setRemark("订单结算第三方支付普通用户增加待发现金");
				freezeCashLog.setType(2);

				if (!(cash.compareTo(BigDecimal.ZERO) == 0)) {
					addFreezeCashLog(account, user, order, freezeCashLog, freezeCash);
				}
			}

			accountShop.setOnlineTurnover(order.getTotalMoney());// accountshop的营业额要加，不区分普通还是非普通流程，也不记流水
			// 更新account中freezeCash金额
			accountService.updateFreezeCash(account);
			// 更新线上营业额
			accountService.updateFormalCash(userAccount);
			accountShopService.updateOnlineTurnover(accountShop);

		}

		// 华宝结算
		if (payType == 3 || payType == 6) {
			// LogUtil.info(LogCodeType.CODE_7002, "======华宝结算======kaiyuanbao:"
			// + kaiyuan.intValue());

			// int shopId = order.getShopId();
			Account account = accountService.getByUserId(order.getUserId());
			account.setOnlineExpenditure(order.getTotalMoney());
			account.setCash(new BigDecimal(0));
			account.setTotalCash(new BigDecimal(0));
			accountService.updateFormalCash(account);

			BigDecimal freezeKaiyuan = accountShop.getFreezeKaiyuan();
			accountShop.setFreezeKaiyuan(kaiyuan);
			accountShop.setOnlineTurnover(order.getTotalMoney());
			// mapper不见了
			accountShopService.updateFreezeKiyuan(accountShop);

			LogKaiyuanFreeze freezeKaiyuanLog = new LogKaiyuanFreeze();
			freezeKaiyuanLog.setType(1);
			freezeKaiyuanLog.setKaiyuanType(1);
			freezeKaiyuanLog.setKaiyuan(kaiyuan);
			freezeKaiyuanLog.setRemark("普通用户华宝结算增加冻结积分");
			// 记录一条日志
			if (!(kaiyuan.compareTo(BigDecimal.ZERO) == 0)) {
				addKaiyuanFreezeLog(accountShop, user, order, freezeKaiyuanLog, freezeKaiyuan);
			}

		}

	}

	/**
	 * 现金冻结日志
	 * 
	 * @param user
	 * @param order
	 * @param freezeCash
	 * @throws Exception
	 */
	private void addFreezeCashLog(Account account, Users user, Orders order, LogCashFreeze freezeCash,
			BigDecimal preCash) throws Exception {

		freezeCash.setFromId(1);
		freezeCash.setUserId(user.getUserId());
		freezeCash.setOrderNo(order.getOrderNo());
		freezeCash.setPreCash(preCash);
		freezeCash.setDataFlag(1);
		freezeCash.setCreateTime(new Timestamp(System.currentTimeMillis()));

		// 新增一条流水
		logCashFreezeService.add(freezeCash);

	}

	/**
	 * 现金日志
	 * 
	 * @param user
	 * @param order
	 * @param logCash
	 * @throws Exception
	 */
	private void addCashLog(Account account, Users user, Orders order, LogCash logCash, BigDecimal preCash)
			throws Exception {
		logCash.setFromId(0);
		logCash.setUserId(user.getUserId());
		logCash.setOrderNo(order.getOrderNo());
		logCash.setPreCash(preCash);

		logCash.setDataFlag(1);
		logCash.setCreateTime(new Timestamp(System.currentTimeMillis()));

		// 新增一条流水
		logCashService.add(logCash);

	}

	/**
	 * 华宝冻结日志
	 * 
	 * @param accountShop
	 * @param user
	 * @param order
	 * @param freezeKaiyuan
	 * @param preKaiyuan
	 * @throws Exception
	 */
	private void addKaiyuanFreezeLog(AccountShop accountShop, Users user, Orders order, LogKaiyuanFreeze freezeKaiyuan,
			BigDecimal preKaiyuan) throws Exception {

		freezeKaiyuan.setFromId(0);
		freezeKaiyuan.setUserId(user.getUserId());
		freezeKaiyuan.setOrderNo(order.getOrderNo());
		freezeKaiyuan.setPreKaiyuan(preKaiyuan);
		freezeKaiyuan.setDataFlag(1);
		freezeKaiyuan.setCreateTime(new Timestamp(System.currentTimeMillis()));
		// LogUtil.info(LogCodeType.CODE_7002, "======logFreezeKaiyuan======" +
		// freezeKaiyuan.getKaiyuan());
		// 新增一条流水
		logKaiyuanFreezeService.add(freezeKaiyuan);

	}

	/**
	 * 记录业务流水数据
	 * 
	 * @param user
	 * @param order
	 * @param account
	 * @param ordersStockscore
	 * @param o
	 * @throws Exception
	 */
	private void addOrderStockScore(Users user, Orders order, Account account, OrdersStockscore ordersStockscore,
			OrdersExtension o) throws Exception {

		ordersStockscore.setOrderNo(SerialNumberUtils.generateOrderNo("a"));
		ordersStockscore.setUserId(account.getUserId());
		ordersStockscore.setOrderStatus(3);
		ordersStockscore.setPayType(order.getPayType());
		ordersStockscore.setRealMoney(new BigDecimal(0));
		ordersStockscore.setScore(o.getScore().intValue());
		ordersStockscore.setOrderRemarks("库存积分不足订单结算系统自动购买");
		ordersStockscore.setCreateTime(new Date());
		ordersStockscore.setCheckTime(new Date());
		ordersStockscore.setDataFlag(1);

		ordersStockscoreService.add(ordersStockscore);

	}

	/**
	 * 正式华宝日志
	 * 
	 * @param accountShop
	 * @param user
	 * @param order
	 * @param preKaiyuan
	 * @throws Exception
	 */
	private void addKaiyuanTurnOverLog(AccountShop accountShop, Users user, Orders order,
			LogKaiyuanTurnover kaiyuanTurnover, BigDecimal preKaiyuan) throws Exception {

		kaiyuanTurnover.setFromId(0);
		kaiyuanTurnover.setUserId(user.getUserId());
		kaiyuanTurnover.setOrderNo(order.getOrderNo());
		kaiyuanTurnover.setPreKaiyuan(preKaiyuan);
		kaiyuanTurnover.setDataFlag(1);
		kaiyuanTurnover.setCreateTime(new Timestamp(System.currentTimeMillis()));

		// 新增一条流水
		logKaiyuanTurnoverService.add(kaiyuanTurnover);

	}

}
